package com.example.juc.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class Demo01 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

//        CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
//            try {
//                TimeUnit.SECONDS.sleep(5);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("hello");
//        });
//        System.out.println("11111");
//        future.get();


        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+"supplier ->Integer");
            return 1024;
        });
        System.out.println(future2.whenComplete((t,u)->{
            System.out.println("t ->"+t);   //正常返回结果
            System.out.println("u ->"+u);   // 错误信息
        }).exceptionally((e)->{
            System.out.println(e.getMessage());
            return 233;
        }).get());
    }
}
